package com.yuye.excel.easyexcel.handle.vali;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.groups.Default;

import com.alibaba.excel.annotation.ExcelProperty;
import com.yuye.excel.base.config.LanguageTransformationHandler;


public class EasyExcelValiHelper {

    private EasyExcelValiHelper(){}
 
    private static Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
 
    public static <T> List<List<String>> validateEntity(T obj) throws NoSuchFieldException {
    	List<List<String>> resultList = new ArrayList<>();
        
        Set<ConstraintViolation<T>> set = validator.validate(obj, Default.class);
        if (set != null && !set.isEmpty()) {
            for (ConstraintViolation<T> cv : set) {
                Field declaredField = obj.getClass().getDeclaredField(cv.getPropertyPath().toString());
                ExcelProperty annotation = declaredField.getAnnotation(ExcelProperty.class);
                String[] value = annotation.value();
                String content = "";
                if(value != null && value.length > 1) {
                	content = value[1];
                }else {
                	content = value[0];
                }
                
                List<String> list = new ArrayList<>();
                list.add(content);
                list.add(cv.getMessage());
                
                resultList.add(list);
//                result.append(content+cv.getMessage()).append(";");
            }
        }
        return resultList;
    }
}
